"""
Case Type   : 问题回归
Case Name   : 观察修改最大可用模式生效结果是否稳定
Create At   : 2023/02/16
Owner       : cr13
Description :
    1.修改最大可用模式参数most_available_sync为on,查询postgresql.conf是否生效
    2.建表并插入,修改,删除数据
    3.等待一段时间查询postgresql.conf,参数most_available_sync值是否依旧生效
    4.清理环境
Expect      :
    1.修改成功
    2.成功
    3.一直生效
    4.成功
History     :
"""

import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.pri_userdb = Node(node='PrimaryDbUser')
        self.com = Common()
        self.commsh = CommonSH('PrimaryDbUser')
        self.tbname = 't_ddl_index_0260_01'
        self.most_available_sync = self.com.show_param('most_available_sync')
        self.conf_path = os.path.join(macro.DB_INSTANCE_PATH,
                                      'postgresql.conf')

    def test_gs_upgradectl(self):
        text = "----step1:修改最大可用模式参数most_available_sync为on," \
               "查询postgresql.conf是否生效 expect:修改成功----"
        self.log.info(text)
        guc_res = self.commsh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                'most_available_sync=on')
        self.assertTrue(guc_res, '执行失败' + text)
        cmd = f"cat {self.conf_path} | grep most_available_sync"
        self.log.info(cmd)
        result = self.com.get_sh_result(self.pri_userdb, cmd)
        self.assertIn('most_available_sync = on', result, '执行失败' + text)

        text = "----step2:建表并插入,修改,删除数据 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tbname};" \
            f"create table {self.tbname}(c1 int);" \
            f"insert into {self.tbname} select generate_series(1,10000);" \
            f"update {self.tbname} set c1=10;" \
            f"delete from {self.tbname} where c1<100;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result, '执行失败'
                      + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result, '执行失败' +
                      text)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, result, '执行失败' +
                      text)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, result, '执行失败' +
                      text)

        text = "----step3:等待一段时间查询postgresql.conf,参数" \
               "most_available_sync值是否依旧生效 expect:一直生效----"
        self.log.info(text)
        time.sleep(30)
        cmd = f"cat {self.conf_path} | grep most_available_sync"
        self.log.info(cmd)
        result = self.com.get_sh_result(self.pri_userdb, cmd)
        self.assertIn('most_available_sync = on', result, '执行失败' + text)

    def tearDown(self):
        text = '-----step4:清理环境 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tbname} cascade;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        guc_res = self.commsh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'most_available_sync={self.most_available_sync}')
        self.assertTrue(guc_res, '执行失败' + text)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败' +
                      text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
